package com.example.helloredis.redission;

import org.redisson.Redisson;
import org.redisson.api.*;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;

import java.io.IOException;

/**
 * @ClassName: RedissionConnection
 * @Description:
 * @Author: 12071
 * @Date: 2022/6/22 16:17
 */
public class RedissonConnection {
    public static void main(String[] args) throws IOException {
        //使用string序列化方式
        Codec codec = new StringCodec();
        // 1. Create config object
        Config config = new Config();
        config.useSingleServer()
                // use "rediss://" for SSL connection
                .setAddress("redis://127.0.0.1:6397")
                .setPassword("redisWSL998-");

//        // or read config from file
//        config = Config.fromYAML(new File("D:\WorkSpace\IDEASpace\helloredis2\src\main\resources\redisson-config.yml"));

        // 2. Create Redisson instance
        // Sync and Async API
        RedissonClient redisson = Redisson.create(config);

        // 3. Get Redis
        // RKey
        System.out.println("Get Keys from redis");
        RKeys keys = redisson.getKeys();
        keys.getKeys().forEach(key -> {
            System.out.println(key);
        });

        // String
        System.out.println("Get String from redis");
        RBucket<Object> rBucket = redisson.getBucket("str", codec);
        System.out.println((String)rBucket.get());

        // RList implements List
        System.out.println("Get List from redis");
        RList<Object> list = redisson.getList("list", codec);
        System.out.println(list);

        // RMap implements ConcurrentMap
        System.out.println("Get Map from redis");
        RMap<String, Object> map = redisson.getMap("jin", codec);
        System.out.println(map.keySet());
        System.out.println(map.values());
        System.out.println(map.get("name"));
        map.put("language", "java");
        System.out.println(map.get("language"));


        // RSet implements Set
        System.out.println("Get Set from redis");
        RSet<Object> set = redisson.getSet("set", codec);
        System.out.println(set);


        // RsortedSet implements SortedSet
        System.out.println("Get sortedSet from redis");
        RSortedSet<Object> zset = redisson.getSortedSet("zset");
        zset.forEach(item ->{
            System.out.println(item);
        });


        redisson.shutdown();

    }
}
